<template>
  <div>
    <el-form ref="form" v-loading="loading" :model="form" label-width="120px" label-position="left">
      <el-form-item label="版本名称" required>
        <el-input v-model="form.name" />
        <p class="d-text-grey d-size-12" style="line-height: 1.5; margin-top: 4px">设置版本名称，默认为当前版本的版本号</p>
      </el-form-item>
      <el-form-item label="应用包类型" required>
        <el-radio-group v-model="form.pack_type">
          <el-radio :value="0">不上传应用包</el-radio>
          <el-radio :value="1">上传原始包</el-radio>
          <el-radio :value="2">上传加密包【推荐】</el-radio>
        </el-radio-group>
        <p class="d-text-grey d-size-12" style="line-height: 1.5; margin-top: 4px">
          设置要提交的应用包类型，原始包为打包的初始未加密应用包，加密包则将上传加密的应用包
        </p>
      </el-form-item>
      <el-form-item label="更新内容" required>
        <dk-tiny-editor v-model="form.content" />
        <p class="d-text-grey d-size-12" style="line-height: 1.5; margin-top: 4px">
          设置更新内容，非必要请勿使用<b style="color: #000">img标签</b>
        </p>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">立即发布</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { publish, show } from '@/api/addons/version'

export default {
  name: 'Publish',
  props: {
    aid: {
      default: 0,
      type: [Number, String]
    },
    vid: {
      default: 0,
      type: [Number, String]
    }
  },
  data() {
    return {
      loading: false,
      version: {},
      form: {
        name: '',
        pack_type: 0,
        content: ''
      }
    }
  },
  mounted() {
    this.getVersionMsg()
  },
  methods: {
    getVersionMsg() {
      show(this.vid).then((res) => {
        this.version = res.data
        this.form.name = this.version.number
        if (this.version.publish) {
          this.$message.info('当前已经发布过一次，再次发布为修改版本')
          this.form = this.version.publish
          this.form.pack_type = 0
        }
      })
    },
    onSubmit() {
      this.loading = true
      publish(this.vid, this.form)
        .then((res) => {
          this.loading = false
          this.$msgbox({
            title: res.msg,
            message: '版本推送成功，请到对应的地址检查更新记录是否正确',
            showCancelButton: false,
            confirmButtonText: '确定',
            beforeClose: (action, instance, done) => {
              if (action === 'confirm') {
                this.$emit('close')
                done()
              } else {
                done()
              }
            }
          })
        })
        .catch((res) => {
          this.loading = false
          console.log(res)
        })
    }
  }
}
</script>
<style scoped lang="scss"></style>
